<?php 
	include_once("ConnectionManager.php");
	
	date_default_timezone_set('Asia/Ho_Chi_Minh');
	
	function loadUsers($start_position, $row_per_page) {
		$query = "SELECT USER_ID, USER_NAME, USER_FULLNAME, USER_CREATEDATE, USER_PASS, USER_TYPE FROM USER ORDER BY USER_NAME";
		$query .= " LIMIT " . $start_position . ", " . $row_per_page;
		ConnectionManager::getInstance()->setQuery($query);
		return ConnectionManager::getInstance()->query();
	}
	
	function addUser($nick, $name, $accountType, $pass) {
		$currentDate = date("Y-m-d H:i:s");
		$query = "INSERT INTO USER (user_name, user_fullname, user_type, user_pass, user_createdate) ";
		$query .= "VALUES ('$nick', '$name', $accountType, '$pass', '$currentDate')";
		ConnectionManager::getInstance()->setQuery($query);
		ConnectionManager::getInstance()->query();
		//return $query;
	}
	
	function updateUserInfo($uid, $nick, $name, $accountType, $pass) {
		$query = "UPDATE USER SET user_name = '$nick', user_fullname = '$name', user_type = $accountType, user_pass = '$pass' ";
		$query .= "WHERE user_id = $uid";
		ConnectionManager::getInstance()->setQuery($query);
		ConnectionManager::getInstance()->query();
		//return $query;
	}
	
	function deleteUser($id) {
		if (isset($id)) {
			$query = "DELETE FROM USER WHERE USER_ID in (" . $id . ")";
			ConnectionManager::getInstance()->setQuery($query);
			ConnectionManager::getInstance()->query();
		}
	}
	
	function countUser() {
		ConnectionManager::getInstance()->setQuery("SELECT * FROM USER");
		ConnectionManager::getInstance()->query();
		return ConnectionManager::getInstance()->getNumRows();
	}
	
	function getUser($username) {
		$query = "SELECT * FROM USER WHERE USER_NAME = '$username'";
		ConnectionManager::getInstance()->setQuery($query);
		return ConnectionManager::getInstance()->query();
	}
	
	function getUserById($uid) {
		ConnectionManager::getInstance()->setQuery("SELECT USER_ID, USER_NAME, USER_FULLNAME, USER_TYPE, USER_PASS FROM USER WHERE USER_ID = $uid");
		return ConnectionManager::getInstance()->query();
	}
?>
